import 'dart:io';

import 'package:liuxlcommon/util/string_utils.dart';

import 'key_mix_test.dart';

List<String> IgnoreFileNames = ['app.dart', 'comm_key_def', 'key_mix'];

void main(List<String> args) async {
  // var args= [];
  if (args.isEmpty) {
    await targetDirectory(Directory("../lib/app"));
    // await targetDirectory(Directory("../ios/Runner"));
    //修改文件名
    await targetDirectory(Directory("../assets"));
  } else if (args[0] == 're') {
    await targetDirectoryRe(Directory("../lib/app"));
    // await targetDirectoryRe(Directory("../ios/Runner"));
  }
  print("success");
  // convertHx(File("./lib/app/com/http/http_request.dart"));
}

targetDirectory(Directory directory) async {
  final List<FileSystemEntity> entities = directory.listSync();

  for (final entity in entities) {
    if (entity is File) {
      convertHx(entity);
    } else if (entity is Directory) {
      targetDirectory(entity);
    }
  }
}

void convertHx(File file) {
  final RegExp regex = RegExp(r'''['"](.*?)['"].hx''');
  if (file.path.contains('.dart') || file.path.contains('.swift')) {
    String fileContent = file.readAsStringSync();
    final Iterable<RegExpMatch> matches = regex.allMatches(fileContent);
    for (final match in matches) {
      String originalFileName1 = match.group(1) ?? "";
      String orTxt = match.group(0).toString();
      if (originalFileName1.contains("'")) {
        originalFileName1 =
            originalFileName1.substring(originalFileName1.lastIndexOf("'") + 1);
        orTxt = "'$originalFileName1'.hx";
      }
      String newFileName = originalFileName1.hxName;

      if (StringUtils.isNotEmpty(originalFileName1.hx)) {
        print("convert File:${file.path}");
        fileContent = fileContent.replaceAll(orTxt, "'$newFileName'.rhx");
      }
    }
    file.writeAsStringSync(fileContent);
  } else if (file.path.contains(".webp") || file.path.contains(".png")) {
    renameImg(file);
  }
}



void renameImg(File file) async {
  String prefix = "new_";
  if (file.path.contains(prefix)) return;
  try {
    String directory = file.parent.path;
    String newFileName = '$prefix${file.path.split('/').last}';
    File newFile = File('$directory/$newFileName');
    print("newFile: $newFile");
    if (await newFile.exists()) {
      await newFile.delete();
    }
    await file.rename(newFile.path);

  } catch (e) {}
}

targetDirectoryRe(Directory directory) async {
  final List<FileSystemEntity> entities = directory.listSync();

  for (final entity in entities) {
    if (entity is File) {
      convertHxRe(entity);
    } else if (entity is Directory) {
      targetDirectoryRe(entity);
    }
  }
}

void convertHxRe(File file) {
  final RegExp regex = RegExp(r'''['"](.*?)['"].rhx''');
  if (file.path.contains('.dart') || file.path.contains('.swift')) {
    String fileContent = file.readAsStringSync();
    final Iterable<RegExpMatch> matches = regex.allMatches(fileContent);

    for (final match in matches) {
      String originalFileName1 = match.group(1) ?? "";
      String orTxt = match.group(0).toString();
      if (originalFileName1.contains("'")) {
        originalFileName1 =
            originalFileName1.substring(originalFileName1.lastIndexOf("'") + 1);
        orTxt = "'$originalFileName1'.rhx";
      }
      String newFileName = originalFileName1.rhx.reHx;
      // print('n:$newFileName, o:$originalFileName1');
      if (StringUtils.isNotEmpty(newFileName)) {
        print("convert File:${file.path}");
        fileContent = fileContent.replaceAll(orTxt, "'$newFileName'.hx");
      }
    }
    file.writeAsStringSync(fileContent);
  }
}
